#####custom
prefix=/usr/local/mscore
prefix_aarch64=/usr/local/mscore_aarch64
export PKG_CONFIG_PATH=${prefix}/lib/pkgconfig:${prefix_aarch64}/lib/pkgconfig:$PKG_CONFIG_PATH
#########
PROGRAM_NAME=libmslog

OUT_LIB=out/lib
OUT_BIN=out/bin
OUT_INCLUDE=out/include/$(PROGRAM_NAME)

OUT_JNISRC=out/$(PROGRAM_NAME)jni_source
OUT_JNILIB=$(OUT_JNISRC)/lib
OUT_JNIINCLUDE=$(OUT_JNISRC)/include

D_BASEAPI_FLAGS= \
	-DOS_LINUX_X64 \
	-D__STDC_FORMAT_MACROS

CFLAGS_SHARE=-shared  -fPIC
C_SRC=src/mslog.c
C_INCLUDE=${D_BASEAPI_FLAGS} -Isrc


mslog_api_sample_CC=${CC}
mslog_api_sample_SRC=${C_SRC} mslog_api_sample.c
	
mslog_api_sample_CFLAGS=${C_INCLUDE}

libmslog_CC=${CC}
libmslog_SRC=${C_SRC}
libmslog_CFLAGS=${C_INCLUDE} ${CFLAGS_SHARE}

libmslog_aarch64_CC=aarch64-linux-gnu-gcc
libmslog_aarch64_SRC=${C_SRC}
libmslog_aarch64_CFLAGS=${C_INCLUDE} ${CFLAGS_SHARE}


all: libmslog install

libmslog: 
	mkdir -p $(OUT_LIB)
	mkdir -p $(OUT_INCLUDE)
	cp -fr src/*.h $(OUT_INCLUDE)
	$($@_CC)  $($@_SRC)  $($@_CFLAGS)  -o $(OUT_LIB)/$(PROGRAM_NAME).so 
gdbs: 
	mkdir -p $(OUT_LIB)
	mkdir -p $(OUT_INCLUDE)
	cp -fr src/*.h $(OUT_INCLUDE)
	$(libmslog_CC)  $(libmslog_SRC)  $(libmslog_CFLAGS) -g -o $(OUT_LIB)/$(PROGRAM_NAME).so  

mslog_api_sample: 
	mkdir -p $(OUT_BIN)
	$($@_CC)  $($@_SRC)  $($@_CFLAGS)  -o $(OUT_BIN)/$@ 
	
install:
	mkdir -p  ${prefix_aarch64}/lib/ 
	mkdir -p  ${prefix_aarch64}/include/   
	mkdir -p  ${prefix_aarch64}/lib/pkgconfig/ 
	cp -fr $(OUT_LIB)/$(PROGRAM_NAME).so          ${prefix}/lib/     
	cp -fr $(OUT_INCLUDE) 	                      ${prefix}/include/	     
	cp -fr $(PROGRAM_NAME).pc		      ${prefix}/lib/pkgconfig/
install_gdb:
	mkdir -p  ${prefix_aarch64}/lib_gdb/ 
	mkdir -p  ${prefix_aarch64}/include/   
	mkdir -p  ${prefix_aarch64}/lib/pkgconfig/ 
	cp -fr $(OUT_LIB)/$(PROGRAM_NAME).so          ${prefix}/lib_gdb/     
	cp -fr $(OUT_INCLUDE) 	                      ${prefix}/include/	     
	cp -fr $(PROGRAM_NAME).pc		      ${prefix}/lib/pkgconfig/
uninstall:
	rm -fr ${prefix}/lib/$(PROGRAM_NAME).so 
	rm -fr ${prefix}/include/$(PROGRAM_NAME)
	rm -fr ${prefix}/lib/pkgconfig/$(PROGRAM_NAME).pc
	
aarch64:libmslog_aarch64 install_aarch64

libmslog_aarch64: 
	mkdir -p $(OUT_LIB)
	mkdir -p $(OUT_INCLUDE)
	cp -fr src/*.h $(OUT_INCLUDE)
	$($@_CC)  $($@_SRC)  $($@_CFLAGS)  -o $(OUT_LIB)/$(PROGRAM_NAME).so 
	
install_aarch64: 
	mkdir -p  ${prefix_aarch64}/lib/ 
	mkdir -p  ${prefix_aarch64}/include/   
	mkdir -p  ${prefix_aarch64}/lib/pkgconfig/ 
	cp -fr $(OUT_LIB)/$(PROGRAM_NAME).so      ${prefix_aarch64}/lib/     
	cp -fr $(OUT_INCLUDE) 	                  ${prefix_aarch64}/include/	     
	cp -fr $(PROGRAM_NAME)_aarch64.pc         ${prefix_aarch64}/lib/pkgconfig/
	
uninstall_aarch64:
	rm -fr ${prefix_aarch64}/lib/$(PROGRAM_NAME).so 
	rm -fr ${prefix_aarch64}/include/$(PROGRAM_NAME)
	rm -fr ${prefix_aarch64}/lib/pkgconfig/$(PROGRAM_NAME)_aarch64.pc		
clean:
	rm  out -fr
help:
	@echo "USAGE:make target"
	@echo "    libmslog      	Produce the ${PROGRAM_NAME}.so"
	@echo "    mslog_api_sample     Sample program with ${PROGRAM_NAME}"
	@echo "    install      	Install ${PROGRAM_NAME} with *.pc."
	@echo "    uninstall     	Uninstall ${PROGRAM_NAME}"
	@echo "    aarch64              Produce the ${PROGRAM_NAME}.so and install it on arm64 platform"
	@echo "    libmsauthent_aarch64 Produce the ${PROGRAM_NAME}.so on arm64 platform"
	@echo "    install_aarch64      Install ${PROGRAM_NAME}  on arm64 platform"
	@echo "    uninstall_aarch64    Uninstall ${PROGRAM_NAME}  on arm64 platform"
	@echo "    clean                Clear out"
